/*
** init.c for Init in /home/vriely/Work/Unix/Tp_Ftp
**
** Made by Samuel Vrielynck
** Login   <@epitech.net>
**
** Started on  Mon Apr 02 15:33:25 2012 Samuel Vrielynck
** Last update Thu Jul 19 17:47:35 2012 pierrick druart
*/

#include		<netdb.h>
#include		<stdio.h>
#include		<sys/types.h>
#include		<sys/socket.h>
#include		<stdlib.h>

#include		"server.h"

static int		init_bind(int sockfd, struct sockaddr *sin, int size)
{
  if (bind(sockfd, sin, size) == -1)
  {
    perror("Error in bind");
    return (0);
  }
  if (listen(sockfd, MAX_CLIENT) == -1)
  {
    perror("Error in listen");
    return (0);
  }
  return (1);
}

int			init(const char *port_s)
{
  struct protoent	*pe;
  struct sockaddr_in	sin;
  int			sockfd;

  sockfd = -1;
  if ((pe = getprotobyname("TCP")) != NULL)
  {
    if ((sockfd = socket(AF_INET, SOCK_STREAM, pe->p_proto)) != -1)
    {
      sin.sin_port = htons(atoi(port_s));
      sin.sin_family = AF_INET;
      sin.sin_addr.s_addr = htonl(INADDR_ANY);
      if (!init_bind(sockfd, (struct sockaddr *) &sin, sizeof(sin)))
	sockfd = -1;
    }
    else
      perror("Error in socket");
  }
  else
    fprintf(stderr, "Erreur in getprotobyname for protocole TCP\n");
  return (sockfd);
}

t_game			*new_game()
{
  t_game		*game;

  game = (t_game *)malloc(sizeof(t_game));
  if (game != NULL)
  {
    game->coord.x = 0;
    game->coord.y = 0;
    game->event = new_list();
    game->team = new_list();
    game->event_mnt = new_list();
    game->sleep = 0;
    game->maxfd = 0;
    game->serverfd = 0;
    game->time = 100;
    game->graphic = NULL;
  }
  return (game);
}
